﻿#pragma execution_character_set("utf-8")
// 本文件为utf-8 编码格式

#include "ImageFilterViewController.h"
#include "../Util/GDUtil.h"
#include "../Util/GDImageFilterUtil.h"
#include "../GdDefine.h"

ImageFilterViewController::ImageFilterViewController()
:scrollView(NULL) // 滚动条
, imageView(NULL) // 显示的图片
, baseImage(NULL) // 原图
, selectImageNode(NULL) // 选择图片节点
, brightNode(NULL) // 亮度节点
, brightValueLabel(NULL) // 亮度值
, brightSlider(NULL) // 亮度滑动条
, contrastNode(NULL) // 对比度节点
, contrastValueLabel(NULL) // 对比度值
, contrastSlider(NULL) // 对比度滑动条
, hueNode(NULL) // 色相节点
, hueValueLabel(NULL) // 色相值
, hueSlider(NULL) // 色相滑动条
, commitNode(NULL) // 提交节点
, btmNode(NULL) // 底部节点
, bright(0) // 亮度值[-1, 1]
, saturation(0)
, contrast(0) // 对比度[-1, 1]
, hue(0) // 色相[0,359]
, imageCount(4)
{

}

ImageFilterViewController::~ImageFilterViewController()
{

}

void ImageFilterViewController::viewDidLoad()
{
	CCSize winSize = this->getView()->getBounds().size;

	scrollView = CAScrollView::createWithFrame(this->getView()->getBounds());
	this->getView()->addSubview(scrollView);
	scrollView->setHorizontalScrollEnabled(false);
	scrollView->setShowsScrollIndicators(false);
	scrollView->setBounceHorizontal(false);

	imageView = CAImageView::createWithFrame(CCRect(0, 0, winSize.width, winSize.width));
	scrollView->addSubview(imageView);

	selectImageNode = CAView::createWithFrame(CCRect(0, imageView->getBounds().size.height + imageView->getFrameOrigin().y + _px(20), winSize.width, _px(60)));
	scrollView->addSubview(selectImageNode);

	for (int i = 1; i <= imageCount; i++){
		CAButton *btn = CAButton::createWithFrame(CCRect((i - 1)*((winSize.width) / imageCount) + _px(10), 0, (winSize.width - _px(60)) / imageCount, _px(40)), CAButtonTypeRoundedRect);
		btn->setAllowsSelected(true);
		btn->setTitleForState(CAControlStateAll, CCString::createWithFormat("图片%d", i)->m_sString);
		btn->setTitleColorForState(CAControlStateSelected, CAColor_red);
		btn->setTag(i);
		btn->addTarget(this, CAControl_selector(ImageFilterViewController::onSelectImageBtnClick), CAControlEventTouchUpInSide);
		selectImageNode->addSubview(btn);
	}

	// 亮度节点
	brightNode = CAView::createWithFrame(CCRect(0, selectImageNode->getBounds().size.height + selectImageNode->getFrameOrigin().y, winSize.width, _px(80)));
	scrollView->addSubview(brightNode);
	CALabel *brightLabel = CALabel::createWithFrame(CCRect(_px(10), _px(20), _px(200), _px(40)));
	brightNode->addSubview(brightLabel);
	brightLabel->setFontSize(_px(26));
	brightLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	brightLabel->setColor(CAColor_black);
	brightLabel->setText("亮　度：");
	brightSlider = CASlider::createWithCenter(CCRect(winSize.width / 2, _px(40), winSize.width * 2 / 3, _px(10)));
	brightSlider->addTarget(this, CAControl_selector(ImageFilterViewController::brightSliderValueChange));
	brightSlider->setValue(0.5);
	brightNode->insertSubview(brightSlider, 2);
	brightValueLabel = CALabel::createWithFrame(CCRect(winSize.width - _px(210), _px(20), _px(200), _px(40)));
	brightNode->addSubview(brightValueLabel);
	brightValueLabel->setFontSize(_px(26));
	brightValueLabel->setTextAlignment(CATextAlignmentRight);
	brightValueLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	brightValueLabel->setColor(CAColor_black);
	brightValueLabel->setText("0");

	// 对比度节点
	contrastNode = CAView::createWithFrame(CCRect(0, brightNode->getBounds().size.height + brightNode->getFrameOrigin().y, winSize.width, _px(80)));
	scrollView->addSubview(contrastNode);
	CALabel *contrastLabel = CALabel::createWithFrame(CCRect(_px(10), _px(20), _px(200), _px(40)));
	contrastNode->addSubview(contrastLabel);
	contrastLabel->setFontSize(_px(26));
	contrastLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	contrastLabel->setColor(CAColor_black);
	contrastLabel->setText("对比度：");
	contrastSlider = CASlider::createWithCenter(CCRect(winSize.width / 2, _px(40), winSize.width * 2 / 3, _px(10)));
	contrastSlider->addTarget(this, CAControl_selector(ImageFilterViewController::contrastSliderValueChange));
	contrastSlider->setValue(0.5);
	contrastNode->insertSubview(contrastSlider, 2);
	contrastValueLabel = CALabel::createWithFrame(CCRect(winSize.width - _px(210), _px(20), _px(200), _px(40)));
	contrastNode->addSubview(contrastValueLabel);
	contrastValueLabel->setFontSize(_px(26));
	contrastValueLabel->setTextAlignment(CATextAlignmentRight);
	contrastValueLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	contrastValueLabel->setColor(CAColor_black);
	contrastValueLabel->setText("0");

	// 饱和度节点
	saturationNode = CAView::createWithFrame(CCRect(0, contrastNode->getBounds().size.height + contrastNode->getFrameOrigin().y, winSize.width, _px(80)));
	scrollView->addSubview(saturationNode);
	CALabel *saturationLabel = CALabel::createWithFrame(CCRect(_px(10), _px(20), _px(200), _px(40)));
	saturationNode->addSubview(saturationLabel);
	saturationLabel->setFontSize(_px(26));
	saturationLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	saturationLabel->setColor(CAColor_black);
	saturationLabel->setText("饱和度：");
	saturationSlider = CASlider::createWithCenter(CCRect(winSize.width / 2, _px(40), winSize.width * 2 / 3, _px(10)));
	saturationSlider->addTarget(this, CAControl_selector(ImageFilterViewController::saturationSliderValueChange));
	saturationSlider->setValue(0.5);
	saturationNode->insertSubview(saturationSlider, 2);
	saturationValueLabel = CALabel::createWithFrame(CCRect(winSize.width - _px(210), _px(20), _px(200), _px(40)));
	saturationNode->addSubview(saturationValueLabel);
	saturationValueLabel->setFontSize(_px(26));
	saturationValueLabel->setTextAlignment(CATextAlignmentRight);
	saturationValueLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	saturationValueLabel->setColor(CAColor_black);
	saturationValueLabel->setText("0");

	// 色相节点
	hueNode = CAView::createWithFrame(CCRect(0, saturationNode->getBounds().size.height + saturationNode->getFrameOrigin().y, winSize.width, _px(80)));
	scrollView->addSubview(hueNode);
	CALabel *hueLabel = CALabel::createWithFrame(CCRect(_px(10), _px(20), _px(200), _px(40)));
	hueNode->addSubview(hueLabel);
	hueLabel->setFontSize(_px(26));
	hueLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	hueLabel->setColor(CAColor_black);
	hueLabel->setText("色　相：");
	hueSlider = CASlider::createWithCenter(CCRect(winSize.width / 2, _px(40), winSize.width * 2 / 3, _px(10)));
	hueSlider->addTarget(this, CAControl_selector(ImageFilterViewController::hueValueChange));
	hueSlider->setValue(0);
	hueNode->insertSubview(hueSlider, 2);
	hueValueLabel = CALabel::createWithFrame(CCRect(winSize.width - _px(210), _px(20), _px(200), _px(40)));
	hueNode->addSubview(hueValueLabel);
	hueValueLabel->setFontSize(_px(26));
	hueValueLabel->setTextAlignment(CATextAlignmentRight);
	hueValueLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	hueValueLabel->setColor(CAColor_black);
	hueValueLabel->setText("0");

	// 平衡R
	cbrNode = CAView::createWithFrame(CCRect(0, hueNode->getBounds().size.height + hueNode->getFrameOrigin().y, winSize.width, _px(80)));
	scrollView->addSubview(cbrNode);
	CALabel *cbrLabel = CALabel::createWithFrame(CCRect(_px(10), _px(20), _px(200), _px(40)));
	cbrNode->addSubview(cbrLabel);
	cbrLabel->setFontSize(_px(26));
	cbrLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	cbrLabel->setColor(CAColor_black);
	cbrLabel->setText("平衡R：");
	cbrSlider = CASlider::createWithCenter(CCRect(winSize.width / 2, _px(40), winSize.width * 2 / 3, _px(10)));
	cbrSlider->addTarget(this, CAControl_selector(ImageFilterViewController::cbrValueChange));
	cbrSlider->setValue(0.5);
	cbrNode->insertSubview(cbrSlider, 2);
	cbrValueLabel = CALabel::createWithFrame(CCRect(winSize.width - _px(210), _px(20), _px(200), _px(40)));
	cbrNode->addSubview(cbrValueLabel);
	cbrValueLabel->setFontSize(_px(26));
	cbrValueLabel->setTextAlignment(CATextAlignmentRight);
	cbrValueLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	cbrValueLabel->setColor(CAColor_black);
	cbrValueLabel->setText("0");

	// 平衡G
	cbgNode = CAView::createWithFrame(CCRect(0, cbrNode->getBounds().size.height + cbrNode->getFrameOrigin().y, winSize.width, _px(80)));
	scrollView->addSubview(cbgNode);
	CALabel *cbgLabel = CALabel::createWithFrame(CCRect(_px(10), _px(20), _px(200), _px(40)));
	cbgNode->addSubview(cbgLabel);
	cbgLabel->setFontSize(_px(26));
	cbgLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	cbgLabel->setColor(CAColor_black);
	cbgLabel->setText("平衡G：");
	cbgSlider = CASlider::createWithCenter(CCRect(winSize.width / 2, _px(40), winSize.width * 2 / 3, _px(10)));
	cbgSlider->addTarget(this, CAControl_selector(ImageFilterViewController::cbgValueChange));
	cbgSlider->setValue(0.5);
	cbgNode->insertSubview(cbgSlider, 2);
	cbgValueLabel = CALabel::createWithFrame(CCRect(winSize.width - _px(210), _px(20), _px(200), _px(40)));
	cbgNode->addSubview(cbgValueLabel);
	cbgValueLabel->setFontSize(_px(26));
	cbgValueLabel->setTextAlignment(CATextAlignmentRight);
	cbgValueLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	cbgValueLabel->setColor(CAColor_black);
	cbgValueLabel->setText("0");

	// 平衡G
	cbbNode = CAView::createWithFrame(CCRect(0, cbgNode->getBounds().size.height + cbgNode->getFrameOrigin().y, winSize.width, _px(80)));
	scrollView->addSubview(cbbNode);
	CALabel *cbbLabel = CALabel::createWithFrame(CCRect(_px(10), _px(20), _px(200), _px(40)));
	cbbNode->addSubview(cbbLabel);
	cbbLabel->setFontSize(_px(26));
	cbbLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	cbbLabel->setColor(CAColor_black);
	cbbLabel->setText("平衡B：");
	cbbSlider = CASlider::createWithCenter(CCRect(winSize.width / 2, _px(40), winSize.width * 2 / 3, _px(10)));
	cbbSlider->addTarget(this, CAControl_selector(ImageFilterViewController::cbbValueChange));
	cbbSlider->setValue(0.5);
	cbbNode->insertSubview(cbbSlider, 2);
	cbbValueLabel = CALabel::createWithFrame(CCRect(winSize.width - _px(210), _px(20), _px(200), _px(40)));
	cbbNode->addSubview(cbbValueLabel);
	cbbValueLabel->setFontSize(_px(26));
	cbbValueLabel->setTextAlignment(CATextAlignmentRight);
	cbbValueLabel->setVerticalTextAlignmet(CAVerticalTextAlignmentCenter);
	cbbValueLabel->setColor(CAColor_black);
	cbbValueLabel->setText("0");


	// 确定按钮
	commitNode = CAView::createWithFrame(CCRect(0, cbbNode->getBounds().size.height + cbbNode->getFrameOrigin().y, winSize.width, _px(140)));
	scrollView->addSubview(commitNode);
	CAButton *commitBtn = CAButton::createWithCenter(CCRect(winSize.width / 2, _px(30), winSize.width - _px(40), _px(50)), CAButtonTypeRoundedRect);
	commitNode->addSubview(commitBtn);
	commitBtn->setTitleForState(CAControlStateAll, "确定");
	commitBtn->addTarget(this, CAControl_selector(ImageFilterViewController::onCommitBtnClick), CAControlEventTouchUpInSide);

	CAButton *resetBtn = CAButton::createWithCenter(CCRect(winSize.width / 2, _px(100), winSize.width - _px(40), _px(50)), CAButtonTypeRoundedRect);
	commitNode->addSubview(resetBtn);
	resetBtn->setTitleForState(CAControlStateAll, "重置");
	resetBtn->addTarget(this, CAControl_selector(ImageFilterViewController::onResetBtnClick), CAControlEventTouchUpInSide);

	// 底部节点
	btmNode = CAView::createWithFrame(CCRect(0, commitNode->getBounds().size.height + commitNode->getFrameOrigin().y, winSize.width, _px(20)));
	scrollView->addSubview(btmNode);

	scrollView->setViewSize(CCSize(winSize.width, btmNode->getFrameOrigin().y + btmNode->getBounds().size.height));

	changeMainImage(1);
}

void ImageFilterViewController::changeMainImage(int tag)
{
	std::string imagePath = CCString::createWithFormat("%s%d.jpg", GD_IMAGE_PATH.c_str(), tag)->m_sString;
	baseImage = CAImage::create(imagePath);
	imageView->setImage(baseImage);

	for (int i = 1; i <= imageCount; i++){
		((CAButton*)selectImageNode->getSubviewByTag(i))->setControlState(tag == i ? CAControlStateSelected : CAControlStateNormal);
	}
}

void ImageFilterViewController::onSelectImageBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	changeMainImage(control->getTag());
}

void ImageFilterViewController::brightSliderValueChange(CAControl* btn, CCPoint point)
{
	CASlider* p_Slider = (CASlider*)btn;
	bright = p_Slider->getValue() - 0.5;
	brightValueLabel->setText(CCString::createWithFormat("%.3f", bright)->m_sString);
}

void ImageFilterViewController::contrastSliderValueChange(CAControl* btn, CCPoint point)
{
	CASlider* p_Slider = (CASlider*)btn;
	contrast = p_Slider->getValue() - 0.5;
	contrastValueLabel->setText(CCString::createWithFormat("%.3f", contrast)->m_sString);
}

void ImageFilterViewController::saturationSliderValueChange(CAControl* btn, CCPoint point)
{
	CASlider* p_Slider = (CASlider*)btn;
	saturation = p_Slider->getValue() * 2 - 1;
	saturationValueLabel->setText(CCString::createWithFormat("%.3f", saturation)->m_sString);
}


void ImageFilterViewController::hueValueChange(CAControl* btn, CCPoint point){
	CASlider* p_Slider = (CASlider*)btn;
	hue = p_Slider->getValue() * 359;
	hueValueLabel->setText(CCString::createWithFormat("%d", hue)->m_sString);
}

void ImageFilterViewController::cbrValueChange(CAControl* btn, CCPoint point){
	CASlider* p_Slider = (CASlider*)btn;
	cbr = p_Slider->getValue() * 200 - 100;
	cbrValueLabel->setText(CCString::createWithFormat("%d", cbr)->m_sString);
}
void ImageFilterViewController::cbgValueChange(CAControl* btn, CCPoint point)
{
	CASlider* p_Slider = (CASlider*)btn;
	cbg = p_Slider->getValue() * 200 - 100;
	cbgValueLabel->setText(CCString::createWithFormat("%d", cbg)->m_sString);
}
void ImageFilterViewController::cbbValueChange(CAControl* btn, CCPoint point)
{
	CASlider* p_Slider = (CASlider*)btn;
	cbb = p_Slider->getValue() * 200 - 100;
	cbbValueLabel->setText(CCString::createWithFormat("%d", cbb)->m_sString);
}

void ImageFilterViewController::viewDidUnload()
{
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

void ImageFilterViewController::changeImage()
{
	CCLog("bright:%f,contrast:%f,saturation:%f,hue:%d,cbr:%d,cbg:%d,cbb:%d", bright, contrast, saturation, hue, cbr, cbg, cbb);
	// 亮度对比度
	CAImage *newImage = GDImageFilterUtil::BCSHFilter(baseImage, bright, contrast, saturation, hue, cbr, cbg, cbb);

	imageView->setImage(newImage);
}

void ImageFilterViewController::onCommitBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	changeImage();
}

void ImageFilterViewController::onResetBtnClick(CrossApp::CAControl *control, CrossApp::CCPoint point)
{
	brightSlider->setValue(0.5);
	contrastSlider->setValue(0.5);
	saturationSlider->setValue(0.5);
	hueSlider->setValue(0);
	cbrSlider->setValue(0.5);
	cbgSlider->setValue(0.5);
	cbbSlider->setValue(0.5);

	brightSliderValueChange(brightSlider, CCPointZero);
	contrastSliderValueChange(contrastSlider, CCPointZero);
	saturationSliderValueChange(saturationSlider, CCPointZero);
	hueValueChange(hueSlider, CCPointZero);
	cbrValueChange(cbrSlider, CCPointZero);
	cbgValueChange(cbgSlider, CCPointZero);
	cbbValueChange(cbbSlider, CCPointZero);

	changeImage();
}